#include "stdafx.h"
#include "keysbox.h"
#include "keyschedule.h"

void KeySchedule( const unsigned char in_key[] , const  int round_counter , unsigned char out_key[] )
{
	int pos[80] = {0} ;
	unsigned char in_keysbox[4] ;
	unsigned char out_keysbox[4] ;
	for( int i = 0 ; i <= 79 ; i++ )
	{
		pos[i] = (i+19)%80 ; 
	}
	for ( int i = 0 ; i <= 79 ; i++ )
	{	
		out_key[i] = in_key[pos[i]] ;
	}

	for ( int i = 0 ; i <=3 ; i++ )
	{
		in_keysbox[i] = out_key[76+i] ;
	}
	key_sbox( in_keysbox , out_keysbox ) ;
	for ( int i = 0 ; i <=3 ; i++ )
	{
		out_key[i+76] = out_keysbox[i] ;
	}
	unsigned char round[5] ;
	for ( int i = 0 ; i <= 4 ; i++ )
	{
		round[i] = (round_counter >> i) & 0x01 ; 
		out_key[i+15] = out_key[i+15] ^ round[i] ;
	}
}